class Ini < IniFile
  
  class Option
    attr_reader :name, :values, :defaultValue
    def initialize(name,values=:any,defval=:none)
      @name=name
      @values=values
      @defaultValue=defval
    end
  end
  def load(file)
    super(file)
    if @hhxData.nil? == false
      @hhxData.each { |key,value|
         @hhxData[key] = nil
         value.each { |vkey, vvalue|
           value[vkey] = nil
           value[vkey.upcase] = vvalue
         }
         @hhxData[key.upcase]=value
      }
    end
  end
  def initialize(file="")
    @options={}
    if file!=nil and file!="" and File.file?(file)
      load(file)
    end
  end
  def loadOption(option)
    if not option.values===:any
      option.values.each_index { |i|
        option.values[i]=option.values[i].to_s.upcase!
      }
    end
    @options[option.name.upcase]=Array.new.push(option.values).push(option.defaultValue)
  end
  def newOption(name, values=:any, default=:none)
    if not values===:any
      values.each_index { |i|
        values[i]=values[i].to_s.upcase!
      }
    end
    @options[name.upcase]=Array.new.push(values).push(default)
  end
  def validate
    section=""
    option=""
    valid=false
    @options.each { |name,attrs|
      section=name.slice(0,name.index("::"))
      option=name.slice(name.index("::")+2,name.size)
      exists = @hhxData.key?(section) and @hhxData[section].key?(option)
      if not exists
        if attrs[1]===:none
          raise OptionMissing.error(@xIniFile,-1,section,option)
        else
          @hhxData[section] = { option => attrs[1] }
        end
      end
      next if attrs[0]===:any
      next unless @hhxData.key?(section) and @hhxData[section].key?(option)
      valid=false
      attrs[0].each { |value|
        if @hhxData[section][option].upcase==value
          valid=true
          break
        end
      }
      raise BadOptionValue.error(@xIniFile,-1,section,option, @hhxData[section][option]) unless valid
    }
  end
  def optionIsEqual?(section,option,value)
    section.upcase!; option.upcase!
    if @hhxData.key?(section) and @hhxData[section].key?(option)
      rvalue=@hhxData[section][option].upcase
      value.upcase!
    else
      return false
    end
    if value==rvalue 
      return true
    else
=begin
      srvalue=rvalue; svalue=value
      rvalue=rvalue.to_i; value=value.to_i
  #    if rvalue==value
    #    return true
      if (value==1 and srvalue=="TRUE") or (svalue=="TRUE" and rvalue==1)
        return true
      elsif (value==0 and srvalue=="FALSE") or (svalue=="FALSE" and rvalue==0)
        return true
      end
=end
      return false
    end
  end
  def each
    if @hhxData!=nil
      @hhxData.each { |key, value|
        yield key, value
      }
    else
      yield nil
    end
  end
end